package hyl.core.info;

import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Content extends CNote {
	public static final String CODE = "CODE";
	public static final String MSG = "MSG";
	public static final String HEAD = "HEAD";
	public static final String BODY = "BODY";
	Map<String, String> head = new HashMap<String, String>();
	Map<String, Object> body = new HashMap<String, Object>();

	public Content(Integer code, String msg) {
		super(code, msg);
	}

	public Content() {
		super();
	}

	public void iniHeads(Map<String, String> heads) {
		this.head = heads;
	}

	public void putHead(String key, String value) {
		head.put(key, value);
	}

	public void removeHead(String key) {
		head.remove(key);
	}

	/**
	 * 
	 * @Title: setBody @Description: 设置消息体 @param body 消息内容 @return void
	 *         返回类型 @throws
	 */
	public void iniBodys(Map<String, Object> body) {
		this.body = body;
	}

	public void addBodyItem(String key, Object value) {
		this.body.put(key, value);
	}

	public void addBodys(Map<String, Object> body) {
		this.body.putAll(body);
	}

	public void removeBody(String key) {
		body.remove(key);
	}

	/**
	 * 
	 * @Title: get @Description:返回消息 @return Map<String,Map<String,String>>
	 *         返回消息 @throws
	 */
	Map<String, Object> hm = new HashMap<String, Object>();

	public Map<String, Object> get() {
		hm.clear();
		hm.put(CODE, code);
		hm.put(MSG, msg);
		if (head != null && !head.isEmpty())
			hm.put(HEAD, head);
		if (body != null && !body.isEmpty())
			hm.put(BODY, body);
		return hm;
	}

	@SuppressWarnings("unchecked")
	public void set(Map<String, Object> map) {
		this.code = (Integer) map.get(CODE);
		this.msg = (String) map.get(MSG);
		this.head = (Map<String, String>) map.get(HEAD);
		this.body = (Map<String, Object>) map.get(BODY);
	}

	public String toJsonString() {
		return JSONObject.toJSONString(get());
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append('{').append('\"').append(CODE).append('\"').append(':').append(code).append(',').append('\"')
				.append(MSG).append('\"').append(':').append('\"').append(msg).append('\"').append(',').append('\"')
				.append(HEAD).append('\"').append(':').append(head).append(',').append('\"').append(BODY).append('\"')
				.append(':').append(body).append('}');
		return sb.toString();

	}

	public byte[] toJsonBytes() {
		return JSON.toJSONBytes(get());
	}

	public void parseBytes(byte[] bytes) {
		set(JSONObject.parseObject(bytes, Map.class));
	}
}
